package com.mp3s.backtest_a.utils;


import java.util.List;

import com.mp3s.db_sqlite_pool.utils.sqliteImplpool;
import com.mp3s.interface3.IBS;
import com.mp3s.interface3.IBackTestSchedule;
import com.mp3s.interface3.QuoteData;
import com.mp3s.interface3.TradeTime;

import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.Task;

public class schedule implements IBackTestSchedule{

    private String gTaskName = "sh000000";           //stock id
    private String gCron = "*/50 * * * * ?";     //10秒
    private sqliteImplpool si = new sqliteImplpool();

    
    public void CronTask(String taskName, List<String>stockList,String cronExpression,IBS ibs) {
        CronUtil.schedule(taskName, cronExpression, new Task() {
            @Override
            public void execute() {
                // 执行任务逻辑
                // System.out.println(""+DateTime.now()+","+taskId);

                if(false == TradeTime.isStrategyTime()){
                    return;
                }

                //读取历史数据
                for(String item:stockList){
                    // System.out.println(item);
                    List<QuoteData> qs = si.findByStockId(item);
                    //BS结果
                    ibs.BS(qs);

                    // for(QuoteData q:qs){
                    //     System.out.println("history:"+q.toString());
                    // }
                }
            }
        });
    }

    public void init(String taskName,List<String>stockList,String cron,IBS ibs) {
        // TODO Auto-generated method stub
        CronUtil.setMatchSecond(true);
        if(cron.length() < 1){
            cron = gCron;
        }
        if(taskName.length() < 1){
            taskName = gTaskName;
        }
        CronTask(taskName,stockList,cron,ibs);
    }

    @Override
    public void start() {
        // 下载调度已经开启，无需再开启
        if(CronUtil.getScheduler().isStarted()==false)
            CronUtil.start();
        
    }

    @Override
    public void stop() {
        if(CronUtil.getScheduler().isStarted()==true)
            CronUtil.stop();
    }

}
